package com.example.administrator.music;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Administrator on 2018/3/30.
 */

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView zizhuzc,wjmm;
    public static boolean zhuc=false;
    private EditText PgNo,paword;
    private TextView btn_login;
    private String pasword,phNo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initview();
    }
    private void initview() {
        zizhuzc= (TextView) findViewById(R.id.log_zhuce);
        wjmm= (TextView) findViewById(R.id.log_mima);
        PgNo= (EditText) findViewById(R.id.usname);
        paword= (EditText) findViewById(R.id.paword);
        btn_login= (TextView) findViewById(R.id.btn_login);

        btn_login.setOnClickListener(this);
        zizhuzc.setOnClickListener(this);
        wjmm.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if(zhuc) {
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.log_zhuce:
                Intent i=new Intent();
                i.setClass(this,ZhucheActivity.class);
                startActivity(i);
                break;
            case R.id.log_mima:
                Intent i1=new Intent();
                i1.setClass(LoginActivity.this,WangjimimaActivity.class);
                startActivity(i1);
                break;
            case R.id.btn_login:
                Toast.makeText(LoginActivity.this, "正在登录，请稍候" , Toast.LENGTH_SHORT).show();
                phNo= String.valueOf(PgNo.getText());
                 pasword= String.valueOf(paword.getText());
                if (phNo.equals("") || pasword.equals("") ){
                    Toast.makeText(LoginActivity.this,"电话号码或密码不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    File file1 = new File("/data/data/com.example.administrator.music/files/musicusername");
                    File file2 = new File("/data/data/com.example.administrator.music/files/musicuserobjectId");
                    if(file1.exists()){
                        file1.delete();
                    }
                    if(file2.exists()){
                        file2.delete();
                    }
                    BmobQuery<MusicPlayUser> query=new BmobQuery<MusicPlayUser>();
                    query.addWhereEqualTo("phongNo",phNo);
                    query.findObjects(new FindListener<MusicPlayUser>() {
                        @Override
                        public void done(List<MusicPlayUser> list, BmobException e) {
                            if (e==null) {
                                Toast.makeText(LoginActivity.this, "查询成功共" + list.size() + "条记录", Toast.LENGTH_SHORT).show();
                                for (MusicPlayUser user:list){
                                    String pw=user.getPassword();
                                    if(pw.equals(pasword)){
                                        String id=user.getObjectId();
                                        String un=user.getUsername();
                                        saveMassage(id,"musicuserobjectId");
                                        saveMassage(un,"musicusername");
                                        MenuActivity.zhuc=true;
                                        MainActivity.theme=user.getTheme();
                                        MainActivity.playmode=user.getMode();
                                        finish();
                                    }
                                }
                            }else {
                                Toast.makeText(LoginActivity.this, "登录失败："+e , Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                break;
        }

    }
    public void saveMassage(String inputText,String textname) {//inputText为传入的要保存的数据
        FileOutputStream out = null;
        BufferedWriter writer = null;
        try {
            //textname为文件名，第二个参数为文件操作模式：文件已经存在，就往文件里面追加类容，不从新创建文件。
            out = openFileOutput(textname, Context.MODE_APPEND);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(inputText);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
